<?php

namespace App\Admin\Controllers\XinYou;

use App\Http\Controllers\Controller;
use App\Model\Guild\GuildAccount;
use App\Model\Guild\GuildBalance;
use App\Model\Guild\GuildHostManage;
use App\Model\Guild\GuildWithdrawal;
use App\Model\GuildManage;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class GuilBalanceController extends Controller
{
    public function index(){
        $this->isAdmin = Admin::user()->inRoles(['administrator']);
        return Admin::content(function (Content $content) {
            $content->header('推广员账户数据');
            $content->body($this->guildList());
        });
    }

    private function guildList()
    {
        $grid = new Grid(new GuildBalance());

        $where = [
            // ['status', Withdraw::STATUS_ING]
        ];

        $adminId = Admin::user()->id;
        if($adminId == 50 || $adminId == 51 ){
            $ghId= GuildManage::getGuidIdByGuildUserId($adminId);

            $guidIds = GuildManage::getGuidIdByAdminId($adminId);
            $grid->model()->where($where)->whereIn('guild_manage_id',$guidIds)->orderBy('id', 'desc');
        }else{
            $grid->model()->where($where)->orderBy('id', 'desc');
        }



        $grid->id('记录ID');
        $grid->guildProfile()->guild_id('推广员id');
        $grid->guildProfile()->name('推广员名');
        $grid->userProfile()->user_profile_id('用户ID')->display(function ($data) {
            return "{$data}（内部ID：{$this->user_id}）";
        });
        $grid->userProfile()->nick_name('推广员昵称');
        $grid->totalBalance('总余额(RMB;元)')->display(function (){
            return bcadd($this->have_access_balance ,$this->audit_access_balance,2);
        });

        $grid->have_access_balance('可提现余额(RMB;元)');
        $grid->audit_access_balance('审核中余额(RMB;元)');

        $grid->accountNum('账户数')->display(function (){
            return GuildAccount::getNoDelAccountCount($this->user_id);
        });

        $grid->historyWithdrawal('已提现金额')->display(function (){
            return GuildWithdrawal::getTotalHistoryWithdrawalByUserId($this->user_id);
        });

        $grid->beingWithdrawal('提现审核中金额')->display(function (){
            return GuildWithdrawal::getTotalBeingWithdrawalByUserId($this->user_id);
        });

        //禁用导出
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableCreateButton();

        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('guildProfile.guild_id', '推官员ID');
            });

            $filter->column(1 / 3, function ($filter) {
                $filter->equal('guildProfile.id', '推广员名')->select('/get_guilds');
            });

        });

        return $grid;
    }
}
